import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { addDeathGuy, addDeathGuyDiagnosis, addDeathGuyStaffs, deathGuyInfo } from "@/api/ImmigrationDictionary";
import moment from "moment";
import PatientInformation from "@/components/ViewComponents/PatientInformation";
import Toolbar from "@/components/ViewComponents/Toolbar";
import { useTranslation } from "react-i18next";
import "./index.less";
import { message } from "antd";

let patientId = "";

export default function Index() {
	const { t } = useTranslation();

	const schemasMeeting: FormSchema[] = [
		{
			label: "1.Presiding Officer",
			field: "hostId",
			component: "Input"
		},
		{
			label: "2.Secretary",
			field: "aideId",
			component: "Input"
		},
		{
			label: "3.Integrated Planning Department",
			field: "deptId",
			component: "Input"
		},
		{
			field: "Direct Care Staff",
			component: "Input",
			slot: "label1",
			colProps: { span: 24 }
		},
		{
			label: "1./",
			field: "Integrated_1_1",
			component: "Input",
			colProps: { span: 12 }
		},
		{
			label: "Position 1",
			field: "Integrated_1_2",
			component: "Input",
			colProps: { span: 12 }
		},
		{
			label: "2./",
			field: "Integrated_2_1",
			component: "Input",
			colProps: { span: 12 }
		},
		{
			label: "Position 2",
			field: "Integrated_2_2",
			component: "Input",
			colProps: { span: 12 }
		},
		{
			label: "3./",
			field: "Integrated_3_1",
			component: "Input",
			colProps: { span: 12 }
		},
		{
			label: "Position 3",
			field: "Integrated_3_2",
			component: "Input",
			colProps: { span: 12 }
		},
		{
			label: "4./",
			field: "Integrated_4_1",
			component: "Input",
			colProps: { span: 12 }
		},
		{
			label: "Position 4",
			field: "Integrated_4_2",
			component: "Input",
			colProps: { span: 12 }
		}
	];
	const [registerFormMeeting, { validateFields: validateFieldsMeeting }] = useForm({
		labelWidth: 120,
		schemas: schemasMeeting,
		baseColProps: { span: 8 }
	});
	const schemasMedicalRecord: FormSchema[] = [
		{
			label: "Lý do vào",
			field: "adReason",
			component: "InputTextArea"
		},
		{
			label: "Chẩn đoán",
			field: "rxId",
			component: "Input"
		},
		{
			label: "Lâm sàng",
			field: "patSymptom",
			component: "InputTextArea"
		},
		{
			label: "Hướng điều trị",
			field: "trtmtObject",
			component: "InputTextArea"
		},
		{
			label: "Diễn biến của BN",
			field: "treatProgress",
			component: "InputTextArea"
		},
		{
			label: "Tinh thần và thái độ phục vụ",
			field: "serviceAttitude",
			component: "InputTextArea"
		},
		{
			label: "Chết ngày",
			field: "dateDeath",
			component: "DatePicker",
			componentProps: {
				showTime: true
			}
		},
		{
			label: "Khi BN chết có mặt",
			field: "witnessId",
			component: "InputTextArea"
		},
		{
			label: "Nguyên nhân chết",
			field: "rxDiagnosis",
			component: "InputTextArea"
		},
		{
			label: "Chẩn đoán GPBL",
			field: "patSymptom2",
			component: "InputTextArea"
		},
		{
			field: "label2",
			component: "Input",
			slot: "label2",
			colProps: { span: 24 }
		},
		{
			label: "Khuyết điểm",
			field: "rxDefect",
			component: "InputTextArea",
			colProps: { span: 24 }
		}
	];
	const [registerFormRecord, { validateFields: validateFieldsRecord }] = useForm({
		labelWidth: 120,
		schemas: schemasMedicalRecord,
		baseColProps: { span: 12 }
	});

	/** Function */

	const handlerToolbarAction = (key: string) => {
		switch (key) {
			case "save":
				handleAdd();
				break;
		}
	};

	const patientInfoSuccess = (val: any) => {
		if (val) {
			patientId = val.patientId;
			getDeathGuyInfo();
		}
	};

	/** Request */

	const getDeathGuyInfo = async () => {
		await deathGuyInfo({ patId: patientId });
		// const formInfo2 = {
		// 	patId: patId,
		// 	fullName: res.patients?.fullName,
		// 	birthyear: res.patients?.birthyear,
		// 	gender: res.patients?.gender,
		// 	occupation: res.patients?.occupation,
		// 	dateAdmission: moment(res?.dateAdmission).format("YYYY-MM-DD"),
		// 	dcDtime: moment(res.discharges?.dcDtime).format("YYYY-MM-DD"),
		// 	addrCity: res.patients?.addrCity
		// };
	};

	const handleAdd = async () => {
		try {
			const recordValues = await validateFieldsRecord();
			const meetingValues = await validateFieldsMeeting();
			const keys = ["Integrated_1", "Integrated_2", "Integrated_3", "Integrated_4"];
			const staffList = keys.map((key: string) => ({
				staffType: meetingValues[`${key}_1`],
				staffId: meetingValues[`${key}_2`],
				patId: patientId
			}));
			await addDeathGuyStaffs(staffList);
			const diagnosisId = await addDeathGuyDiagnosis({
				patientId,
				rxId: recordValues.rxId,
				patSymptom: recordValues.patSymptom
			});
			const deathReport = {
				hostId: meetingValues.hostId,
				aideId: meetingValues.aideId,
				deptId: meetingValues.deptId,
				patId: patientId,
				treatProgress: recordValues.treatProgress,
				serviceAttitude: recordValues.serviceAttitude,
				dateDeath: moment(recordValues.dateDeath, "YYYY-MM-DD HH:mm:ss").valueOf(),
				witnessId: recordValues.witnessId,
				rxId: diagnosisId,
				rxDefect: recordValues.rxDefect
			};
			console.log("死亡报告查看", deathReport);
			await addDeathGuy(deathReport);
			message.success("submit success!");
		} catch (err) {
			console.log(err);
			message.error("submit fail!");
		}
	};

	return (
		<div className="page509 page">
			<div className="page-content">
				<div className="block">
					<PatientInformation successFn={patientInfoSuccess} />
				</div>
				<div className="flex mt-2">
					<div className="block">
						<div className="block-label">Ⅱ.{t("flandre_ui.meeting_participants")}</div>
						<BasicForm register={registerFormMeeting}>
							<div slot="label1">4.Direct Care Staff</div>
						</BasicForm>
					</div>
					<div className="block ml-2">
						<div className="block-label">Ⅲ.{t("flandre_ui.review_content")}</div>
						<BasicForm register={registerFormRecord}>
							<div slot="label2" className="block-label">
								Ⅳ.Deficiencies/Shortcomings
							</div>
						</BasicForm>
					</div>
				</div>
			</div>
			<Toolbar action={handlerToolbarAction} btnList={[{ key: "save" }]} />
		</div>
	);
}
